package com.lzj.server.admin.controller;

import com.lzj.server.admin.service.AdminService;
import com.lzj.server.admin.entity.Permission;
import com.lzj.server.admin.entity.Role;
import com.lzj.server.entity.video.Category;
import com.lzj.server.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/vimi/admin")
@Tag(name = "后台管理模块")
public class AdminController {

    private final AdminService adminService;


    AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    /**
     * 添加角色
     */
    @Operation(summary = "添加角色")
    @PostMapping("/addRole")
    public Result<?> addRole(Role role) {
        adminService.addRole(role);
        return Result.success();
    }

    /**
     * 添加权限
     */
    @Operation(summary = "添加权限")
    @PostMapping("/addPermission")
    public Result<?> addPermission(@RequestBody Permission permission) {
        adminService.addPermission(permission);
        return Result.success();
    }

    /**
     * 删除角色 or 权限
     */
    @Operation(summary = "删除角色 or 权限")
    @DeleteMapping("/deleteRoleOrPermission")
    @Validated
    public Result<?> deleteRoleOrPermission(@RequestParam("id") @NonNull Long id,
                                            @RequestParam("type") @NonNull Integer typeNum) {
        adminService.deleteRoleOrPermission(id, typeNum);
        return Result.success();
    }

    /**
     * 获取角色列表(所有)
     */
    @Operation(summary = "获取角色列表")
    @GetMapping("/getRoleList")
    public Result<List<Role>> getRoleList() {
        return Result.success(adminService.getRoleList());
    }

    /**
     * 获取权限列表(所有)
     */
    @Operation(summary = "获取权限列表")
    @GetMapping("/getPermissionList")
    public Result<List<Permission>> getPermissionList() {
        return Result.success(adminService.getPermissionList());
    }

    /**
     * 配置角色权限
     */
    @Operation(summary = "配置角色权限")
    @PostMapping("/configPermission")
    @Validated
    public Result<?> configPermission(@RequestBody List<Long> permissionIdList,
                                      @RequestParam("roleId") @NonNull Long roleId) {
        adminService.configPermission(permissionIdList, roleId);
        return Result.success();
    }

    /**
     * 配置用户角色
     */
    @Operation(summary = "配置用户角色")
    @PostMapping("/configRole")
    @Validated
    public Result<?> configRole(@RequestBody List<Long> roleIdList,
                                @RequestParam("userId") @NonNull Long userId) {
        adminService.configRole(roleIdList, userId);
        return Result.success();
    }
    /**
     * 禁用用户
     */
    @Operation(summary = "禁用用户")
    @PutMapping("/disableUser")
    @Validated
    public Result<?> disableUser(@RequestParam("userId") @NonNull Long userId) {
        return Result.success();
    }

    /**
     *  添加分类
     */
    @Operation(summary = "添加分类")
    @PostMapping("/addCategory")
    @Validated
    public Result<Boolean> addCategory(@RequestBody Category category) {
        return Result.success(adminService.addOrEditCategory(category));
    }
}
